package com.oliet.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author jiaZhenpeng
 * @Title: PasswordUtils
 * @Package com.oliet.util
 * @Description: 密码工具类
 * @date 2023-12-03 10:14
 */
@Slf4j
public class PasswordUtils {

    private static final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * 生成加密密码
     *
     * @param rawPassword
     * @return
     */
    public static String generateEncryptionPassword(String rawPassword) {
        // 使用BCryptPasswordEncoder加密密码
        String encodedPassword = passwordEncoder.encode(rawPassword);
        log.info("Raw Password: " + rawPassword);
        log.info("Encoded Password: " + encodedPassword);
        return encodedPassword;
    }

    /**
     * 校验密码
     *
     * @param rawPassword 原始密码
     * @param encodedPassword 加密后的密码
     * @return 如果密码正确返回true，否则返回false
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        // 使用BCryptPasswordEncoder校验密码
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
}
